package com.nave.segundaguerra.activity;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.nave.segundaguerra.audio.SoundManager;
import com.nave.segundaguerra.servidorecliente.cliente.ControleDeUsuarioCliente;
import com.nave.segundaguerra.servidorecliente.util.ActivityUtil;
import com.nave.segundaguerra.servidorecliente.util.Connection;
import com.nave.segundaguerra.servidorecliente.util.ProcuraServers;
import com.nave.segundaguerra.servidorecliente.util.Tools;

public class ActivityListaIps extends ListActivity {

//	private ArrayList<ServerList> serversList;

//	private ProcuraServers procura;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		Tools.enableMainConfig(this);
		SoundManager.getInstance().ResumeSong("MenuSound");
		
//		serversList = new ArrayList<ServerList>();
		
		ActivityUtil.setCurrentActivity(this);
		
//		procura = new ProcuraServers(this);
//		procura.execute();
		setListAdapter(new GenIpsList(this, ProcuraServers.getInstance().getList()));
		
		Button procurarButton = new Button(ActivityUtil.getCurrentActivity());
		procurarButton.setText("Procurar Servidor");
		procurarButton.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View view) {
	        	ProcuraServers.getInstance().cancelPesquisa();
	        	try {
	    			Thread.sleep(200);
	    		} catch (InterruptedException e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    		}
	            ProcuraServers.getInstance().execute();
	        }
	    });
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT, 
				LinearLayout.LayoutParams.WRAP_CONTENT);


		addContentView(procurarButton, params);
		
	}
	
	//Modify by Nuno Duarte
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		
		ServerListComponent server = ProcuraServers.getInstance().getList().get(position);
		ProcuraServers.getInstance().cancelPesquisa();
		try {
			Socket s = new Socket(server.getIP(), ConnectionSet.getPort());
			ConnectionSet.setConnection(new Connection(s, 
					GameInformation.getNamePlayer(),
					//GameInformation.getPlayer().getName(),
					new ControleDeUsuarioCliente()));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ActivityUtil.ChangeActivity(GameplayActivity.class);
						
		
	}
	
}
